﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OracleClient;
using System.Data;
using System.Text;
using Xiaov.SmartWeb.Model;

namespace Xiaov.SmartWeb.Application.TZGC
{
    public partial class GC : BasePage
    {
        private string _moduleid;
        public string ModuleID
        {
            get
            {
                if (Request.QueryString["mid"] != null)
                {
                    _moduleid = Request.QueryString["mid"].ToString();
                }
                else
                {
                    _moduleid = "0000";
                }
                return _moduleid;
            }
        }

        //权限检查
        protected void Page_Init(object sender, EventArgs e)
        {
            PM_Filter();
        }

        //页面加载
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {                
                //绑定grid
                BindGrid();
            }
        }

        //绑定GRID
        protected void BindGrid()
        {
            StringBuilder sql_query = new StringBuilder();
            sql_query.Append(" select c.*, decode(c.wczt, 0, '未完成', '已完成') as zt,substr(gclb,2,length(gclb))as gclbmc,b.xmmc, c.rowid  ");
            sql_query.Append(" from yt_gcb c,yt_xmb b  ");
            sql_query.Append(" where c.ssxmbm  = b.xmid(+)  ");
            if (!string.IsNullOrEmpty(TextBox1.Text.ToString().Trim()))
            {
                sql_query.Append(" and c.gcmc like '%" + TextBox1.Text.ToString().Trim() + "%'  ");
            }

            if (DropDownList1.SelectedValue != "-1")
            {
                sql_query.Append(" and c.wczt = '" + DropDownList1.SelectedValue.ToString() + "'  ");
            }

            if (DropDownList2.SelectedValue != "0全部")
            {
                sql_query.Append(" and c.gclb = '" + DropDownList2.SelectedValue.ToString() + "'  ");
            }

            sql_query.Append(" order by c.gcmc ");

            GridView1.DataSource = Maticsoft.DBUtility.DbHelperOra.Query(sql_query.ToString());
            GridView1.DataBind();
        }

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindGrid();
        }

        protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindGrid();
        }

        protected void Button6_Click(object sender, EventArgs e)
        {
            BindGrid();
        }

        protected void btn_save_Click(object sender, EventArgs e)
        {
             
            if (String.IsNullOrEmpty(hfGCID.Value.ToString()))  //新增
            {
                string gcid,gcbm,gcmc,gclb,wczt;          
                gcbm = txtgcmc.Text.ToString().Trim();
                gcmc = txtgcmc.Text.ToString().Trim();
                gclb = DropDownList3.SelectedValue.ToString();
                wczt = cbSFWC.Checked ? "1" : "0";
                string sql_query;
                sql_query = "select decode(max(to_number(gcid)),null,1,max(to_number(gcid))+1) from yt_gcb";
                gcid = Convert.ToString(Maticsoft.DBUtility.DbHelperOra.GetSingle(sql_query));
                string sql_insert;    
                sql_insert = @"insert into yt_gcb (GCID,GCBM,GCMC,SSXMBM,SX,GCLB,WCZT) 
                                    values ('{0}','{1}','{2}','{3}','{4}','{5}','{6}')";
                sql_insert = string.Format(sql_insert,gcid,gcbm,gcmc,null,null,gclb,wczt);
                Maticsoft.DBUtility.DbHelperOra.ExecuteSql(sql_insert);
                this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "提示", "<Script Language='JavaScript'>window.alert('保存成功！')</script>");
                BindGrid();
                ClearTextbox();
               
            }
            else   //更新
            {
                string gcid, gcbm, gcmc, gclb, wczt;
                gcid = hfGCID.Value.ToString();
                gcbm = txtgcmc.Text.ToString().Trim();
                gcmc = txtgcmc.Text.ToString().Trim();
                gclb = DropDownList3.SelectedValue.ToString();
                wczt = cbSFWC.Checked ? "1" : "0";
                //更新项目表
                string sql_update;
                sql_update = @"update yt_gcb set gcbm = '{0}',gcmc  = '{1}',gclb  = '{2}', wczt  = '{3}' where gcid = '{4}' ";
                sql_update = string.Format(sql_update, gcbm, gcmc, gclb, wczt, gcid);
                Maticsoft.DBUtility.DbHelperOra.ExecuteSql(sql_update);
                BindGrid();
                this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "提示", "<Script Language='JavaScript'>window.alert('保存成功！')</script>");
                ClearTextbox();
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            ClearTextbox();
        }

        private void ClearTextbox()
        {

            hfGCID.Value = null;
            txtgcmc.Text = "";
            cbSFWC.Checked = false;
            btn_save.Text = "增加";
        }

        
        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex = e.NewPageIndex;//分页BindData();
            BindGrid();
        }

        //操作事件 编辑/改变状态/删除
        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            hfGCID.Value = e.CommandArgument.ToString();
            switch (e.CommandName)
            {
                case "bianji":
                   string gcid_bianji;
                   gcid_bianji = e.CommandArgument.ToString();
                   StringBuilder sql_query = new StringBuilder();
                   sql_query.Append("select *  ");
                   sql_query.Append(" from yt_gcb c ");
                   sql_query.Append(" where gcid = '" + gcid_bianji + "' ");
                   OracleDataReader mydr;
                   mydr = (OracleDataReader)Maticsoft.DBUtility.DbHelperOra.ExecuteReader(sql_query.ToString());
                   while (mydr.Read())
                   {
                       txtgcmc.Text = mydr["gcmc"].ToString();      
                       cbSFWC.Checked = mydr["wczt"].ToString() == "0" ? false : true;
                       DropDownList3.SelectedValue = mydr["gclb"].ToString(); 
                   }
                   mydr.Close();   
                   btn_save.Text = "保存";                  
                   break;
               case "shanchu":
                   string sql_del;
                   string gcid_shanchu;
                   gcid_shanchu = e.CommandArgument.ToString();     
                   //删除项目表中的记录
                   sql_del = "delete from yt_gcb where gcid =  '" + gcid_shanchu + "'";
                   Maticsoft.DBUtility.DbHelperOra.ExecuteSql(sql_del);
                   this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "提示", "<Script Language='JavaScript'>window.alert('删除成功！')</script>");
                   BindGrid();
                   break;
                default:
                    btn_save.Text = "增加";
                    break;
            }
        }

        //隔行换色
        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                //鼠标移动到某行上，该行变色
                e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#dddddd'");
                //鼠标移开后，恢复
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
                //序号
                e.Row.Cells[0].Text = (e.Row.RowIndex + 1).ToString();
                
            }
        }

        protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
        {
            GridView1.Columns[6].Visible = false;
            GridView1.Columns[7].Visible = false;
            GridView1.Columns[8].Visible = false;
        }

        //工程导入按钮
        protected void Button3_Click(object sender, EventArgs e)
        {
            //OracleParameter[] parameters1 ={ 
            //        new OracleParameter("v_dwbm",OracleType.VarChar,100),
            //        new OracleParameter("v_nf",OracleType.VarChar,100),
            //        new OracleParameter("v_yf",OracleType.VarChar,100)
            //        };
            //parameters1[0].Value = dwbm;
            //parameters1[1].Value = nf;
            //parameters1[2].Value = yf.PadLeft(2, '0');
            //OracleDataReader odr3 = Maticsoft.DBUtility.DbHelperOra.RunProcedure("p_cj_yt_tzzb_compute", parameters1);
            //odr3.Close();
        }
    }
}